package Web实验;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.StringTokenizer;

public class SocketServer {
    public static void main(String[] args) throws IOException {
        //1.建立一个服务器Socket(ServerSocket)绑定指定端口
        ServerSocket serverSocket = new ServerSocket(8800);
        //2.使用accept()方法阻止等待监听，获得新连接
        Socket socket = serverSocket.accept();
        //3.获得输入流
        InputStream inputStream = socket.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        //获得输出流
        OutputStream outputStream = socket.getOutputStream();
        PrintWriter printWriter = new PrintWriter(outputStream);
        //4.读取用户输入信息
        String info = null;
        String result = "";
        int x = 0;
        while (!((info = bufferedReader.readLine()) == null)) {
            String info2 = info.substring(15, info.length());
            StringTokenizer info3 = new StringTokenizer(info2);
            String a;
            int[] info4 = new int[info3.countTokens()];

            while (info3.hasMoreTokens()) {
                a = info3.nextToken();
                int b = Integer.parseInt(a);
                info4[x] = b;
                x++;
            }
           insertSort(info4);
            for(int key:info4)
            {
                result += key+"\t";
            }
              System.out.print(result);
        }
        //给客户一个响应
        String  reply = result ;
        printWriter.write(reply);
        printWriter.flush();
        //5.关闭资源
        printWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
    }
    private static void insertSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int temp = array[i];
            for (int j = i - 1; j >= 0 && temp < array[j]; j--) {
                array[j + 1] = array[j];
                array[j] = temp;
            }
        }
    }
}